package com.easytopit.demo.dao;

import java.sql.Timestamp;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.easytopit.demo.domain.Order;

public interface OrderMapper {
	
	@Update({"update user set balance = balance+#{money,jdbcType=DOUBLE} where name = #{name,jdbcType=VARCHAR}"})
	void addMoney(@Param("name")String name,@Param("money")double money);
	
	@Select({"select balance from user where name = #{name,jdbcType=VARCHAR}"})
	Double userBalance(@Param("name")String name);
	
	@Insert({"insert into user_order (id,user_name,cinema,hall,movie_name,movie_time,version,language,info,quantity,price,money,create_time,end_time,status,pay_status)",
		"values (#{id,jdbcType=VARCHAR},#{userName,jdbcType=VARCHAR},#{cinema,jdbcType=VARCHAR},#{hall,jdbcType=VARCHAR},"
		+ "#{movieName,jdbcType=VARCHAR},#{movieVersion,jdbcType=VARCHAR},#{movieTime,jdbcType=VARCHAR},"
		+ "#{info,jdbcType=VARCHAR},#{quantity,jdbcType=INTEGER},#{price,jdbcType=DOUBLE},#{money,jdbcType=DOUBLE},"
		+ "#{createTime,jdbcType=VARCHAR},#{endTime,jdbcType=VARCHAR},#{orderStatus,jdbcType=VARCHAR},#{payStatus,jdbcType=VARCHAR})"})
	void addOrder(Order order);
	
	@Update({"update user set balance = balance+#{money,jdbcType=DOUBLE} where name = #{name,jdbcType=VARCHAR}"})
	void addBalance(@Param("name")String name,@Param("money")double money);
	
	@Update({"update user set balance = balance-#{money,jdbcType=DOUBLE} where name = #{name,jdbcType=VARCHAR}"})
	void reduceBalance(@Param("name")String name,@Param("money")double money);
	
	@Update({"update user_order set end_time = #{time,jdbcType=VARCHAR},status = '处理成功',pay_status = '已支付' where id = #{id,jdbcType=VARCHAR}"})
	boolean payOrder(@Param("id")String id,@Param("time")String time);
	
	@Update({"update user_order set end_time = #{time,jdbcType=VARCHAR},status = '处理成功',pay_status = '已退订' where id = #{id,jdbcType=VARCHAR}"})
	boolean refundOrder(@Param("id")String id,@Param("time")String time);
	
	@Select({"select * from user_order where id = #{id,jdbcType=VARCHAR}"})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.VARCHAR),
		@Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="cinema", property="cinema", jdbcType=JdbcType.VARCHAR),
        @Result(column="hall", property="hall", jdbcType=JdbcType.VARCHAR),
        @Result(column="movie_name", property="movieName", jdbcType=JdbcType.VARCHAR),
        @Result(column="movie_time", property="movieTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR),
        @Result(column="quantity", property="quantity", jdbcType=JdbcType.INTEGER),
        @Result(column="price", property="price", jdbcType=JdbcType.DOUBLE),
        @Result(column="money", property="money", jdbcType=JdbcType.DOUBLE),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="end_time", property="endTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="orderStatus", jdbcType=JdbcType.VARCHAR),
        @Result(column="pay_status", property="payStatus", jdbcType=JdbcType.VARCHAR)
    })
	Order queryOrderById(@Param("id")String id);
	
	@Select({"select * from user_order where user_name = #{name,jdbcType=VARCHAR}"})
	@Results({
		@Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR),
        @Result(column="cinema", property="cinema", jdbcType=JdbcType.VARCHAR),
        @Result(column="hall", property="hall", jdbcType=JdbcType.VARCHAR),
        @Result(column="movie_name", property="movieName", jdbcType=JdbcType.VARCHAR),
        @Result(column="movie_time", property="movieTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR),
        @Result(column="quantity", property="quantity", jdbcType=JdbcType.INTEGER),
        @Result(column="price", property="price", jdbcType=JdbcType.DOUBLE),
        @Result(column="money", property="money", jdbcType=JdbcType.DOUBLE),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="end_time", property="endTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="orderStatus", jdbcType=JdbcType.VARCHAR),
        @Result(column="pay_status", property="payStatus", jdbcType=JdbcType.VARCHAR)
    })
	List<Order> queryOrderByName(@Param("name")String name);
	
	@SelectProvider(type=OrderMapperSql.class,method="searchOrder")
	@Results({
		@Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR),
        @Result(column="cinema", property="cinema", jdbcType=JdbcType.VARCHAR),
        @Result(column="hall", property="hall", jdbcType=JdbcType.VARCHAR),
        @Result(column="movie_name", property="movieName", jdbcType=JdbcType.VARCHAR),
        @Result(column="movie_time", property="movieTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="info", property="info", jdbcType=JdbcType.VARCHAR),
        @Result(column="quantity", property="quantity", jdbcType=JdbcType.INTEGER),
        @Result(column="price", property="price", jdbcType=JdbcType.DOUBLE),
        @Result(column="money", property="money", jdbcType=JdbcType.DOUBLE),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="end_time", property="endTime", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="orderStatus", jdbcType=JdbcType.VARCHAR),
        @Result(column="pay_status", property="payStatus", jdbcType=JdbcType.VARCHAR)
    })
	List<Order> searchOrder(@Param("name")String name,@Param("id")String id,@Param("payStatus")String payStatus,@Param("createTime")String createTime,@Param("endTime")String endTime,@Param("cinema")String cinema);
}